﻿using B.S.BasicData.Api.Write.Application.Command.User;
using B.S.BasicData.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.S.BasicData.Api.Write.Controllers
{
    /// <summary>
    /// 用户服务
    /// </summary>
    [Route("api/[controller][action]")]
    [ApiController]
    [Authorize]
    public class UserController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ILogger<UserController> logger;

        public UserController(IMediator mediator, ILogger<UserController> logger)
        {
            _mediator = mediator;
            this.logger = logger;
        }
        /// <summary>
        /// 用户新增
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回受影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPost]
        public Task<ApiResult<int>> CreateUser(CreateUserCommand request)
        {
            try
            {
                logger.LogInformation("用户新增成功");
                return _mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("添加失败" + ex.Message);
                throw ex;
            }
            
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpDelete]
        public Task<ApiResult<int>> DeleteUser([FromQuery]DeleteUserCommand request)
        {
            try
            {
                logger.LogInformation("用户删除成功");
                return _mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("删除失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="request">请求参数</param>
        /// <param name="cancellationToken">取消</param>
        /// <returns>返回受影响行数</returns>
        /// <exception cref="NotImplementedException"></exception>
        [HttpPut]
        public Task<ApiResult<int>> UpdateUser(UpdateUserCommand request)
        {
            try
            {
                logger.LogInformation("修改用户信息成功");
                return _mediator.Send(request);
            }
            catch (Exception ex)
            {

                logger.LogError("修改失败" + ex.Message);
                throw ex;
            }
        }
        /// <summary>
        /// 删除单个与员工
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        [HttpDelete]
        public Task<ApiResult<int>> DeleteSingEmp([FromQuery] DeleteUserEmpCommand request)
        {
            return _mediator.Send(request);
        }
    }
}
